// +build !edge

package dbinit

import (
	"iot-base/common/config"
	"iot-base/common/dbconn"
	"iot-base/common/dbobj"
	"iot-base/tm/common/thingapi"
)

func createService(modelID string, name string, stationID string, tid string) {
	var count int64 = 0
	if tid == "" {
		dbconn.DBConnection.Model(&dbobj.Thing{}).Where("model_id=?", "m_tm").Count(&count)
	} else {
		dbconn.DBConnection.Model(&dbobj.Thing{}).Where("uid=?", "tid").Count(&count)
	}

	if count == 0 {
		tm := dbobj.Thing{
			UID:        tid,
			ModelID:    modelID,
			SName:      name,
			IsProxy:    false,
			IsStation:  false,
			StatusUsed: 1,
		}
		dbconn.DBConnection.Create(&tm)
		st := dbobj.ThingStation{
			ThingID:   tm.UID,
			StationID: stationID,
			ModelID:   modelID,
			SNO:       0,
		}
		dbconn.DBConnection.Save(&st)
	}
}

func init() {
	var count int64
	dbconn.DBConnection.Model(&dbobj.Area{}).Where("level=?", "255").Count(&count)
	if count == 0 {
		var area dbobj.Area
		//if config.PType == "edge" {
		area.SName = "云端"
		area.WholeName = "云端"
		area.Level = "255"
		area.UID = "cloud"
		//}

		dbconn.DBConnection.Save(&area)
	}
	var station dbobj.Station
	count = 0
	dbconn.DBConnection.Model(&station).Where("level=?", -1).Count(&count)
	if count == 0 {
		var cloud dbobj.Area
		dbconn.DBConnection.Model(&dbobj.Area{}).Where("level=?", "255").Find(&cloud)
		//var station dbobj.Station
		station.AreaID = cloud.UID
		station.SName = "基础服务"
		station.Level = -1
		thingapi.CreateStation(&station, "m_cloudSide", -1)
	}
	createService("m_tm", "配置管理服务", station.UID, config.SelfOption.ID)
	//createService("m_dm", "数据管理服务", station.UID)
	createService("m_mm", "媒体接入管理服务", station.UID, "")

	//创建配置服务器

}
